package com.example.timeemail.my;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.example.timeemail.R;
import com.example.timeemail.adapter.MyRecyclerAdapter;
import com.example.timeemail.adapter.OnItemClickListener;
import com.example.timeemail.entity.Person;

import java.util.ArrayList;
import java.util.List;

public class DriftActivity extends AppCompatActivity {

    private RecyclerView Rvdrift;

    private MyRecyclerAdapter myAdapter;

    private String[] names = {"李铭","小花","宋妈","张明","姥爷","表哥" };

    private int[] imgs = {R.drawable.tx_1,R.drawable.tx_2,R.drawable.tx_3,
            R.drawable.tx_4,R.drawable.tx_5,R.drawable.tx_6};

    private List<Person> drift = new ArrayList<Person>();

    AlertDialog alert ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drift);

        //初始化数据
        initDataBooks();
        //1.初始化控件
        Rvdrift = findViewById(R.id.rvDrift);
        //2.设置RecyclerView布局管理器
        Rvdrift.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        //3.初始化数据适配器
        myAdapter = new MyRecyclerAdapter(drift,this);
        //4.采用默认动画效果
        Rvdrift.setItemAnimator(new DefaultItemAnimator());
        //5.设置适配器
        Rvdrift.setAdapter(myAdapter);
        //6.设置监听器
        myAdapter.setMonItemClickListener(new OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int postion) {
                //获取调集通讯录的对象
                Person person = drift.get(postion);
                //弹出提示
                int p = postion+1;
                Toast.makeText(DriftActivity.this,"您点击了第"+p+"个通讯录，姓名为"+person.getName(),Toast.LENGTH_LONG).show();
                tos("通讯录","\n姓名："+person.getName()+"\n\n"+"qq："+p+"24542234\n\n"+"微信："+p+"adssafas\n");
            }
        });
    }

    //初始化数据
    private void initDataBooks(){
        for (int i = 0; i < names.length; i++){
            //新建Person对象,存放头像及姓名
            Person person = new Person(names[i],imgs[i]);
            //将数据存入数据列表
            drift.add(person);
        }
    }

    private void tos(String name,String tel){
        alert = new AlertDialog.Builder(this).create();
        alert.setTitle(name);
        alert.setIcon(R.drawable.ic_launcher_foreground);
        alert.setMessage(tel);
        alert.show();
    }

}